//
//  NCForgetPwdVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/1.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "NCForgetPwdVC.h"
#import "BFPaperButton.h"
#import "NSString+Utilities.h"
#import "NSObject+CountDown.h"
@interface NCForgetPwdVC ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (weak, nonatomic) IBOutlet UITextField *codeTf;
@property (weak, nonatomic) IBOutlet BFPaperButton *codeBtn;

@property (weak, nonatomic) IBOutlet UITextField *pwdTf;

@property (weak, nonatomic) IBOutlet UITextField *confirmPwdTf;

@property (nonatomic,  strong) NSString *tem_token;


@end

@implementation NCForgetPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"忘记密码";
    self.view.backgroundColor = kTableView_bg;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.codeBtn cancelTimer];
    
}
- (IBAction)sendCodeAction:(id)sender {
    
    if ([self.phoneTf.text isEmpty]) {
        cwqAlertMessage(@"手机号不能为空");
        return ;
    }
    if (![self.phoneTf.text isMobile]) {
        cwqAlertMessage(@"不合法的手机号~");
        return ;
    }
    
    weakSelf_define;
    [[HELNetworkManager sharedMg] GET:[NSString stringWithFormat:@"%@/%@",helVerificationCodeUrl,self.phoneTf.text] parameters:nil success:^(id responseObject) {
        if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
            weakSelf.tem_token =  responseObject[@"data"][@"xauthToken"];
            cwqAlertMessage(@"验证码发送成功~");
            [weakSelf.codeBtn countDownTime:60 countDownBlock:^(NSUInteger timer) {
                
                
                [weakSelf.codeBtn setBackgroundColor:[UIColor lightGrayColor]];
                [weakSelf.codeBtn setTitle:[NSString stringWithFormat:@"%zd 秒", timer] forState:UIControlStateNormal];
                weakSelf.codeBtn.enabled = NO;
            } outTimeBlock:^{
                [weakSelf.codeBtn setBackgroundColor:HexRGB_WSJ(249, 55, 70)];
                [weakSelf.codeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [weakSelf.codeBtn setTitle:@"验证码" forState:UIControlStateNormal];
                weakSelf.codeBtn.enabled = YES;
                
            }];

        }
      
        
    } failure:^(NSString *err) {
        cwqAlertMessage(@"验证码发送失败");
    }];
    
    
}



- (IBAction)confirmAction:(id)sender {
 
    if ([self.phoneTf.text isEmpty]) {
            cwqAlertMessage(@"手机号不能为空");
            return ;
    }
    if (![self.phoneTf.text isMobile]) {
        cwqAlertMessage(@"不合法的手机号~");
        return ;
    }
  
    
    
        if ([self.pwdTf.text isEmpty] || self.pwdTf.text.length <6 || self.pwdTf.text.length > 16) {
            cwqAlertMessage(@"密码不合法");
            return ;
        }
        if (![self.confirmPwdTf.text isEqualToString:self.pwdTf.text]) {
            cwqAlertMessage(@"两次密码不一致");
            return ;
        }
        cwqHud(self.view);
        weakSelf_define;
        
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
        
  
    params[@"newPassword"] = self.pwdTf.text;
    
    
    //问题未处理
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 30;
  
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer  setValue:self.tem_token forHTTPHeaderField:@"xauthToken"];
    NSString *url =[NSString stringWithFormat:@"%@/%@/%@",[HELNetworkManager sharedMg].bathURL,helForgetPasswordUrl,self.codeTf.text];
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"data"] isKindOfClass:[NSString class]]) {
           
                cwqAlertMessage(responseObject[@"data"]);
                [weakSelf.navigationController popViewControllerAnimated:YES];
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error, id response) {
          [hud hide:YES];
    }];
  
  
    
}



@end
